package models;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author Andriy
 * */

 public class Encrypt {
	public Encrypt(){
	}
	/**
	 * Encrypts input string value using MessageDigest class.
	 *
	 * @param password
	 *            String password in UTF-8 encoded characters
	 * @return New string value for password to store.
	 */
	public static String encrypt(String password) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA");
			md.update(password.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			e.getMessage();
			return null;
		} catch (UnsupportedEncodingException e) {
			e.getMessage();
			return null;
		}
		byte raw[] = md.digest();

		return (new BASE64Encoder()).encode(raw);
	}
	/**
	 * Generates random alpha-numeric password.
	 *
	 * @return Random password string
	 **/
	public static String generatepassword() {
		SecureRandom random = new SecureRandom();
		return new BigInteger(40, random).toString(32);
	}
}
